USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_CLAVE_EMPRESA]    Script Date: 11/24/2011 14:14:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_M0002_CLAVE_EMPRESA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0002_CLAVE_EMPRESA]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_CLAVE_EMPRESA]    Script Date: 11/24/2011 14:14:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_M0002_CLAVE_EMPRESA]
AS
BEGIN
	SELECT	a.aooper
			,a.aosuc
			,a.aocta
			,b.R1cta
	INTO	dbo.base_clave
	FROM	fsd010 a
			,fsr011 b
	WHERE	a.aosbop = 0
	AND		a.aooper <> 99999999
	AND		a.aocta <> 999999999 
	AND		a.aocta = b.R2cta 
	AND		a.aosbop = b.R2sbop 
	AND		a.aooper = b.r2oper 
	AND		a.aosuc = b.r2suc 
	AND		a.aotasa <> 0 
	AND		b.relcod = 505

	SELECT	a.aooper
			,a.aosuc
			,a.aocta
			,a.R1cta
			,b.z07327AGE
			,CAST(RTRIM(CAST(b.Z07327cor AS CHAR)) + REPLICATE('0', 2-LEN(CAST(b.Z07327MAT AS CHAR))) + CAST(b.Z07327MAT AS CHAR) AS INT) AS clave 
	INTO	dbo.clave_emp
	FROM	dbo.base_clave AS a LEFT JOIN z07327 AS b ON a.R1cta = Z07327CTA 
	WHERE	b.z07327EST = 'AC' 
END

GO


